package com.ytjj.qmyx.users.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.utils.IPUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.users.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送短信
 *
 *
 * @version 1.0
 *
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController extends BaseController {

    private final SmsService smsService;

    public SmsController(SmsService smsService) {
        this.smsService = smsService;
    }

    /**
     * 获取短信验证码接口(5分钟后失效)
     *
     * @param phone 电话号码
     * @return
     */
    @GetMapping("/generateAuthCode")
    public CommonResult generateAuthCode(@RequestParam(value = "phone") String phone,
                                         @RequestParam(value = "type") String type) {
        String ip = null;
        try {
            ip = IPUtil.getIpAddress(TokenUtil.getRequest());
            log.info("===========ip={}", ip);
        } catch (Exception e) {
            log.error("===获取ip失败：{}", e);
        }
        return smsService.generateAuthCode(phone, type);
    }

    @GetMapping("/generateAuthCodeV2")
    public CommonResult generateAuthCodeV2(@RequestParam(value = "phone") String phone,
                                         @RequestParam(value = "type") String type) {
        String ip = null;
        try {
            ip = IPUtil.getIpAddress(TokenUtil.getRequest());
            log.info("===========ip={}", ip);
        } catch (Exception e) {
            log.error("===获取ip失败：{}", e);
        }
        return smsService.generateAuthCodeV2(phone, type);
    }
}
